﻿using DeviceCommons.DeviceMessages.Abstractions;
using DeviceCommons.DeviceMessages.Enums;
using DeviceCommons.DeviceMessages.Models;

namespace DeviceCommons.DeviceMessages.Serialization
{
    public class DeviceMessageHeaderParser : AbstractMessageParser<IDeviceMessageHeader>, IDeviceMessageHeaderParser
    {
        public DeviceMessageHeaderParser() : base(new DeviceMessageHeader())
        {

        }

        public override IDeviceMessageHeader Parser(ReadOnlySpan<byte> bytes)
        {
            IDeviceMessageHeader model = new DeviceMessageHeader();

            model.Header = bytes[..2].ToArray();

            model.Version = bytes[2];
            model.TimeStampFormat = (TimeStampFormatEnum)(bytes[3] >> 0 & 0x01);
            model.ValueType = (HeaderValueTypeEnum)(bytes[3] >> 1 & 0x01);
            model.Reserve1 = (Reserve1Enum)(bytes[3] >> 2 & 0x01);
            model.Reserve2 = (Reserve2Enum)(bytes[3] >> 3 & 0x01);
            model.CRCType = (CRCTypeEnum)(bytes[3] >> 4 & 0x07);

            return model;
        }
    }
}
